package br.caixa2.reports;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import br.caixa2.entities.Client;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class ClientListReport {
	private static Font fontTitulo = new Font( Font.HELVETICA, 16, Font.BOLDITALIC );

	public static void report( List<Client> list ) throws IOException {
		File file = File.createTempFile( "accountReport", ".pdf" );
		try {
			Document document = new Document();
			PdfWriter.getInstance( document, new FileOutputStream( file ) );
			document.open();
			addTitlePage( document, "Lista de Clientes\n" );

			PdfPTable accClientTable = new PdfPTable( 4 );
			accClientTable.setWidthPercentage( 100 );
			accClientTable.addCell( new PdfPCell( new Paragraph( "Nome" ) ) );
			accClientTable.addCell( new PdfPCell( new Paragraph( "Cpf" ) ) );
			accClientTable.addCell( new PdfPCell( new Paragraph( "Telefone" ) ) );
			accClientTable.addCell( new PdfPCell( new Paragraph( "Endereço" ) ) );

			for ( Client client : list ) {
				accClientTable.addCell( new PdfPCell( new Paragraph( client.getName() ) ) );
				accClientTable.addCell( new PdfPCell( new Paragraph( client.getCpf() ) ) );
				accClientTable.addCell( new PdfPCell( new Paragraph( client.getPhone() ) ) );
				accClientTable.addCell( new PdfPCell( new Paragraph( client.getAdress() ) ) );
			}
			document.add( accClientTable );
			document.close();
		} catch ( Exception e ) {
			e.printStackTrace();
		}

		java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

		try {
			desktop.open( file );
		} catch ( IOException e ) {
			e.printStackTrace();
		}
	}

	private static void addTitlePage( Document document, String title ) throws DocumentException {
		Paragraph preface = new Paragraph();
		preface.add( new Paragraph( title, fontTitulo ) );
		document.add( preface );
	}
}
